//// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
//package com.linqingying.gitcode.pullrequest.ui.timeline
//
//import com.intellij.collaboration.async.CompletableFutureUtil
//import com.intellij.collaboration.async.stateInNow
//import com.intellij.collaboration.ui.codereview.comment.CodeReviewSubmittableTextViewModelBase
//import com.intellij.collaboration.ui.codereview.comment.CodeReviewTextEditingViewModel
//import com.intellij.collaboration.ui.icon.IconsProvider
//import com.intellij.collaboration.util.ComputedResult
//import com.intellij.collaboration.util.getOrNull
//import com.intellij.openapi.progress.EmptyProgressIndicator
//import com.intellij.openapi.project.Project
//import com.intellij.openapi.util.Disposer
//import com.intellij.platform.util.coroutines.childScope
//import com.intellij.util.io.await
//import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.cancel
//import kotlinx.coroutines.channels.awaitClose
//import kotlinx.coroutines.flow.*
//import com.linqingying.gitcode.api.data.GCReactionContent
//import com.linqingying.gitcode.api.data.GCUser
//import com.linqingying.gitcode.api.data.pullrequest.GCPullRequest
//import com.linqingying.gitcode.pullrequest.comment.convertToHtml
//import com.linqingying.gitcode.pullrequest.data.GCPRDataContext
//import com.linqingying.gitcode.pullrequest.data.GCReactionsService
//import com.linqingying.gitcode.pullrequest.data.provider.GCPRDataProvider
//import com.linqingying.gitcode.pullrequest.data.provider.GCPRDetailsDataProvider
//import com.linqingying.gitcode.pullrequest.ui.details.model.GCPRDetailsFull
//import com.linqingying.gitcode.pullrequest.ui.emoji.GCReactionViewModelImpl
//import com.linqingying.gitcode.pullrequest.ui.emoji.GCReactionsViewModel
//
//class GCPRDetailsTimelineViewModel internal constructor(private val project: Project,
//                                                        parentCs: CoroutineScope,
//                                                        private val dataContext: GCPRDataContext,
//                                                        private val dataProvider: GCPRDataProvider) {
//  private val cs = parentCs.childScope()
//
//  private val currentUser: GCUser = dataContext.securityService.currentUser
//  private val reactionsService: GCReactionsService = dataContext.reactionsService
//  private val reactionIconsProvider: IconsProvider<GCReactionContent> = dataContext.reactionIconsProvider
//
//  val details: StateFlow<ComputedResult<GCPRDetailsFull>> = channelFlow<ComputedResult<GCPRDetailsFull>> {
//    val disposable = Disposer.newDisposable()
//    dataProvider.detailsData.loadDetails(disposable) {
//      if (!it.isDone) {
//        trySend(ComputedResult.loading())
//      }
//      it.handle { res, err ->
//        if (err != null && !CompletableFutureUtil.isCancellation(err)) {
//          trySend(ComputedResult.failure(err.cause ?: err))
//        }
//        else {
//          val details = createDetails(res)
//          trySend(ComputedResult.success(details))
//        }
//      }
//    }
//    awaitClose { Disposer.dispose(disposable) }
//  }.stateIn(cs, SharingStarted.Eagerly, ComputedResult.loading())
//
//  private val _descriptionEditVm = MutableStateFlow<GCPREditDescriptionViewModel?>(null)
//  val descriptionEditVm: StateFlow<GCPREditDescriptionViewModel?> = _descriptionEditVm.asStateFlow()
//
//  private val loadedReactionsState = details.map { it.getOrNull() }.filterNotNull().map { it.reactions }.stateInNow(cs, emptyList())
//  val reactionsVm: GCReactionsViewModel =
//    GCReactionViewModelImpl(cs, dataProvider.id.id, loadedReactionsState, currentUser, reactionsService, reactionIconsProvider)
//
//  private fun createDetails(data: GCPullRequest): GCPRDetailsFull = GCPRDetailsFull(
//    dataProvider.id,
//    data.url,
//    data.author ?: dataContext.securityService.ghostUser,
//    data.createdAt,
//    data.title.convertToHtml(project),
//    data.body,
//    data.body.convertToHtml(project),
//    data.viewerCanUpdate,
//    data.viewerCanReact,
//    data.reactions.nodes
//  )
//
//  fun editDescription() {
//    if (_descriptionEditVm.value != null) return
//    val details = details.value.getOrNull() ?: return
//    val description = details.description.orEmpty()
//    _descriptionEditVm.value = GCPREditDescriptionViewModel(project, cs, dataProvider.detailsData, description) {
//      _descriptionEditVm.update {
//        it?.dispose()
//        null
//      }
//    }.apply {
//      requestFocus()
//    }
//  }
//}
//
//class GCPREditDescriptionViewModel(project: Project,
//                                   parentCs: CoroutineScope,
//                                   private val detailsData: GCPRDetailsDataProvider,
//                                   initialText: String,
//                                   private val onDone: () -> Unit)
//  : CodeReviewSubmittableTextViewModelBase(project, parentCs, initialText), CodeReviewTextEditingViewModel {
//  override fun save() {
//    submit {
//      detailsData.updateDetails(EmptyProgressIndicator(), description = it).await()
//      onDone()
//    }
//  }
//
//  override fun stopEditing() = onDone()
//
//  internal fun dispose() = cs.cancel()
//}
